package comm.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.signature.SignatureMethod;

import statics.TravlogVars;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.http.AccessToken;
import twitter4j.http.OAuthAuthorization;


public class TwitterTest {
	 public static void main(String[] args) throws Exception {
		 
		 Twitter twitter = new TwitterFactory().getInstance("changdoc", "1818714");
		 //twitter.setOAuthConsumer(TravlogVars.twitterConsumerKey, TravlogVars.twitterConsumersecret);
		 AccessToken at = twitter.getOAuthAccessToken();
		 User user = twitter.verifyCredentials();
		 twitter.updateStatus("x Auth Test?");
		 
		 /*
	        OAuthConsumer consumer = new DefaultOAuthConsumer(
	        		TravlogVars.twitterConsumerKey, TravlogVars.twitterConsumersecret,
	                SignatureMethod.HMAC_SHA1);
	        
	        OAuthProvider provider = new DefaultOAuthProvider(consumer,
	                "http://twitter.com/oauth/request_token",
	                "http://twitter.com/oauth/access_token",
	                "http://twitter.com/oauth/authorize");

	        System.out.println("Fetching request token from Twitter...");

	        // we do not support callbacks, thus pass OOB
	        String authUrl = provider.retrieveRequestToken(OAuth.OUT_OF_BAND);

	        System.out.println("Request token: " + consumer.getToken());
	        System.out.println("Token secret: " + consumer.getTokenSecret());

	        System.out.println("Now visit:\n" + authUrl
	                + "\n... and grant this app authorization");
	        System.out.println("Enter the PIN code and hit ENTER when you're done:");

	        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	        String pin = br.readLine();

	        System.out.println("Fetching access token from Twitter...");

	        provider.retrieveAccessToken(pin);

	        System.out.println("Access token: " + consumer.getToken());
	        System.out.println("Token secret: " + consumer.getTokenSecret());

	        URL url = new URL("http://twitter.com/statuses/mentions.xml");
	        HttpURLConnection request = (HttpURLConnection) url.openConnection();

	        consumer.sign(request);

	        System.out.println("Sending request to Twitter...");
	        request.connect();

	        System.out.println("Response: " + request.getResponseCode() + " "
	                + request.getResponseMessage());
	              */
	    }
}
